#include "spriteBox.h"
#include "enums.h"
#include <SFML/Graphics.hpp>
#include <iostream>

spriteBox::spriteBox()
{
	m_texture = 0;
	m_attribs.x = 0;
	m_attribs.y = 0;
}

void spriteBox::setTexture(sf::Texture* p_tex)
{
	m_texture = p_tex;

	for (int i=0; i<4; i++){
		m_sprite_diagonals[i].setTexture(*m_texture);
		m_sprite_diagonals[i].setRotation(90*i);
		m_sprite_diagonals[i].setTextureRect(sf::IntRect(0, 0, 8, 8));
	}

	for (int i=0; i<m_sprite_cardinals[N].size(); i++)
	{
		m_sprite_cardinals[N].at(i).setTexture(*m_texture);
		m_sprite_cardinals[S].at(i).setTexture(*m_texture);
	}

	for (int i=0; i<m_sprite_cardinals[W].size(); i++)
	{
		m_sprite_cardinals[W].at(i).setTexture(*m_texture);
		m_sprite_cardinals[E].at(i).setTexture(*m_texture);
	}
}

void spriteBox::setPosition(int p_x, int p_y)
{
	for (int i=0; i<m_sprite_cardinals[N].size(); i++)
	{
		m_sprite_cardinals[N].at(i).move(-m_attribs.x + p_x, -m_attribs.y + p_y);
		m_sprite_cardinals[S].at(i).move(-m_attribs.x + p_x, -m_attribs.y + p_y);
	}

	for (int i=0; i<m_sprite_cardinals[W].size(); i++)
	{
		m_sprite_cardinals[W].at(i).move(-m_attribs.x + p_x, -m_attribs.y + p_y);
		m_sprite_cardinals[E].at(i).move(-m_attribs.x + p_x, -m_attribs.y + p_y);
	}

	for (int i=0; i<4; i++){
		m_sprite_diagonals[i].move(-m_attribs.x + p_x, -m_attribs.y + p_y);
	}

	m_attribs.x = p_x;
	m_attribs.y = p_y;
}

void spriteBox::setSize(int p_tiles_x, int p_tiles_y)
{
	m_attribs.tiles_x = std::max(p_tiles_x-2, 0); //Subtract 2 to compensate for the edge tiles.
	m_attribs.tiles_y = std::max(p_tiles_y-2, 0); //This is the size of the "entire box", so 2 is 2x2.

	buildBox();
}

void spriteBox::clearBox()
{
	for (int i=0; i<4; i++)
		m_sprite_cardinals[i].clear();
}

void spriteBox::buildBox()
{
	clearBox();

	sf::Sprite newSprite;

	if (m_texture!=0)
		newSprite.setTexture(*m_texture);

	newSprite.setTextureRect(sf::IntRect(8, 0, 8, 8));

	for (int x=0; x<m_attribs.tiles_x; x++)
	{
		newSprite.setScale(1, 1);
		newSprite.setPosition(m_attribs.x + (x+1)*m_tile_size, m_attribs.y);
		m_sprite_cardinals[N].push_back(newSprite);

		newSprite.setScale(1, -1);
		newSprite.setPosition(m_attribs.x + (x+1)*m_tile_size, m_attribs.y + m_attribs.tiles_y*m_tile_size + m_tile_size*2);
		m_sprite_cardinals[S].push_back(newSprite);
	}

	newSprite.setRotation(-90);

	for (int y=0; y<m_attribs.tiles_y; y++)
	{
		newSprite.setScale(1, 1);
		newSprite.setPosition(m_attribs.x, m_attribs.y + (y+1)*m_tile_size + m_tile_size);
		m_sprite_cardinals[W].push_back(newSprite);

		newSprite.setScale(1, -1);
		newSprite.setPosition(m_attribs.x + m_attribs.tiles_x*m_tile_size + m_tile_size*2, m_attribs.y + (y+1)*m_tile_size + m_tile_size);
		m_sprite_cardinals[E].push_back(newSprite);
	}

	m_sprite_diagonals[NW].setPosition(m_attribs.x, m_attribs.y);
	m_sprite_diagonals[NE].setPosition(m_attribs.x + m_attribs.tiles_x*m_tile_size + m_tile_size*2, m_attribs.y);
	m_sprite_diagonals[SW].setPosition(m_attribs.x, m_attribs.y + m_attribs.tiles_y*m_tile_size + m_tile_size*2);
	m_sprite_diagonals[SE].setPosition(m_attribs.x + m_attribs.tiles_x*m_tile_size + m_tile_size*2, m_attribs.y + m_attribs.tiles_y*m_tile_size + m_tile_size*2);
}

void spriteBox::draw(sf::RenderWindow* App)
{
	for (int i=0; i<m_sprite_cardinals[N].size(); i++)
	{
		App->draw(m_sprite_cardinals[N].at(i));
		App->draw(m_sprite_cardinals[S].at(i));
	}

	for (int i=0; i<m_sprite_cardinals[W].size(); i++)
	{
		App->draw(m_sprite_cardinals[W].at(i));
		App->draw(m_sprite_cardinals[E].at(i));
	}

	for (int i=0; i<4; i++)
		App->draw(m_sprite_diagonals[i]);

	sf::RectangleShape testRect;
	testRect.setPosition(m_attribs.x + m_tile_size, m_attribs.y + m_tile_size);
	testRect.setSize(sf::Vector2f(m_attribs.tiles_x * m_tile_size, m_attribs.tiles_y * m_tile_size));
	testRect.setFillColor(sf::Color::Black);
	App->draw(testRect);
}